package com.ghsc.yigou.live.ui
import android.Manifest
import android.content.Intent
import android.text.TextUtils
import androidx.lifecycle.lifecycleScope
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.getCompatColor
import com.cn.appcore.ext.loge
import com.cn.appcore.ext.showToast
import com.cn.appcore.ext.startKtxActivityKill
import com.cn.appcore.http.constant.HttpConstant
import com.cn.appcore.utils.ApkUtil
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.GsonUtil
import com.cn.appcore.utils.Preference
import com.ghsc.yigou.live.R
import com.ghsc.yigou.live.app.App
import com.ghsc.yigou.live.config.CustKey
import com.ghsc.yigou.live.config.UserManager
import com.ghsc.yigou.live.databinding.ActivityLaunchBinding
import com.ghsc.yigou.live.ui.login.LoginActivity
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.permissionx.guolindev.PermissionX
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 项目：YiGouLive
 * 包名：com.ghsc.yigou.live
 * 创建时间：2023/7/31 10:59
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class LaunchActivity : BaseVMActivity<LaunchViewModel, ActivityLaunchBinding>()  {
    override fun attachLayoutRes(): Int {
        return R.layout.activity_launch
    }


    override fun onStart() {
        super.onStart()
        if (!isTaskRoot) {
            val intent = intent
            val intentAction = intent.action;
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(
                    Intent.ACTION_MAIN
                )
            ) {
                finish()
                return
            }
        }
    }


    private var isClick = false //是否进行了点击
    private var isPause = false // 广告被点击后，当前activity是否pause
    private var isOnPause = false //判断是否跳转了广告落地页
    var isShow = false //因为首次安装打开。会走2次
    override fun initView() {
        lifecycleScope.launch{
            delay(1000)
            if (UserManager.getIsFirst()!="1"){
                UserManager.setIsFirst("1")
                App.init()
            } else  UserManager.setIsFirst("1")
            openActivity()
        }


    }

    fun cheAdvData(){
//        lifecycleScope.launch{
//            openActivity()
//        }
//        loadZjAdv(CustKey.zjKaiPing)

        openActivity()
    }


    private fun openActivity() {
        var isLogin: String by Preference(HttpConstant.APP_IS_LOGIN, "")
        if (isLogin=="1"){
            startKtxActivityKill<MainActivity>()
        }else {
            startKtxActivityKill<LoginActivity>()
        }

    }

    override fun attachVMClass(): Class<LaunchViewModel>? {
        return LaunchViewModel::class.java
    }


    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@LaunchActivity) {
                showToast(it)
            }
            againLoginMsg.observe(this@LaunchActivity) {
                showToast(CustKey.IN_AGAIN_MSG)

            }

        }
    }


}